﻿namespace Sidvall.AuthorizationManager.UI.Entities
{
	public partial class ClaimCollection
	{
		#region Deserialize

		public static ClaimCollection Deserialize(string value)
		{
			return Deserialize(value, null);
		}
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		public static ClaimCollection Deserialize(string value, ClaimCollection defaultValue)
		{
			Sidvall.Security.ClaimCollection claims;
			ClaimCollection items;
			Claim item;

			if (string.IsNullOrWhiteSpace(value))
				return defaultValue;
			try
			{
				claims = Sidvall.Serialization.SerializerManager.Current.Deserialize<Sidvall.Security.ClaimCollection>(value);
				items = new ClaimCollection();
				foreach (var claim in claims)
				{
					item = new Claim()
					{
						ClaimType = claim.ClaimType,
						Value = claim.Value,
					};
					items.Add(item);
				}
				return items;
			}
			catch
			{
				return defaultValue;
			}
		}

		#endregion
		#region Serialize

		public string Serialize()
		{
			return Serialize(this);
		}
		public static string Serialize(ClaimCollection items)
		{
			Sidvall.Security.ClaimCollection claims;
			Sidvall.Security.Claim claim;

			if (items == null)
				return null;
			if (items.Count == 0)
				return null;
			claims = new Sidvall.Security.ClaimCollection();
			foreach (var item in items)
			{
				claim = new Sidvall.Security.Claim()
				{
					ClaimType = item.ClaimType,
					Value = item.Value,
				};
				claims.Add(claim);
			}
			return Sidvall.Serialization.SerializerManager.Current.SerializeObject(claims);
		}

		#endregion
	}
}
